﻿using Framework;

namespace Game
{
    public class SceneObjectAttackActionable : IBTreeActionable<SceneObjectBTContext>
    {
        private SceneBattleObjectBTContext _context;
        
        public void OnStart(SceneObjectBTContext context)
        {
            _context = context as SceneBattleObjectBTContext;
            DoAttack(0f);
            Scheduler.Instance.AddTimeListener(SceneConst.AttackInterval, DoAttack);
        }

        public void OnEnd(SceneObjectBTContext context)
        {
            Scheduler.Instance.RemoveTimeListener(DoAttack);
        }

        public TaskStatus Update(SceneObjectBTContext context)
        {
            return TaskStatus.Running;
        }

        private void DoAttack(float deltaTime)
        {
            _context.sceneObject.GetComponent<SceneObjectActionComponent>().SetTrigger(SceneObjectActions.Attack);
            (_context.collidedEntity as SceneBattleEntity).BeHit(_context.sceneObject);
        }
    }
}